package com.example.avmanagerweb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.avmanagerweb.entity.DepKey;
import com.example.avmanagerweb.entity.SysDevic;
import com.example.avmanagerweb.entity.SysDoc;
import com.example.avmanagerweb.entity.SysDoctype;
import com.example.avmanagerweb.entity.vo.FileVo;
import com.example.avmanagerweb.mapper.SysDocMapper;
import com.example.avmanagerweb.result.RLIST;
import com.example.avmanagerweb.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xg
 * @since 2023-06-29
 */
@Service
public class SysDocServiceImpl extends ServiceImpl<SysDocMapper, SysDoc> implements SysDocService {
    @Autowired
    @Lazy
    protected SysDocService sysDocService;
    @Autowired
    @Lazy
    protected SysDevicService sysDevicService;
    @Autowired
    @Lazy
    protected DepKeyService depKeyService;

    @Autowired
    @Lazy
    protected SysDoctypeService sysDoctypeService;
    @Override
    public RLIST


    getSosqryList(FileVo fileVo) {
        String DevNo = fileVo.getDevNo();
        System.out.println(DevNo + "   " + fileVo.getKey());
        SysDevic one = null;
        //参数校验
        if (fileVo.getTimeType() < 0 || fileVo.getTimeType() > 1) {
            return RLIST.error().message("选择时间查询类型 不合法");
        }
        if (StringUtils.isEmpty(fileVo.getKey()) || StringUtils.isEmpty(DevNo)) {
            return RLIST.error().message("key或者DevNo不存在");
        } else {
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("groupkey", fileVo.getKey());
            DepKey list = depKeyService.getOne(wrapper);
            System.out.println(list);
            if (list == null) {
                return RLIST.error().message("key不正确");
            }
            QueryWrapper wrapper2 = new QueryWrapper();
            wrapper2.eq("dvDepartmentID", list.getGroupid());
            wrapper2.eq("dvNum", DevNo);
            one = sysDevicService.getOne(wrapper2);
            System.out.println(one);
            if (one == null) {
                return RLIST.error().message("设备不存在");
            }
        }
        //开始时间
        String begin = fileVo.getBegin();
        System.out.println(begin + "dShootingTime");
        //结束时间
        String end = fileVo.getEnd();
        System.out.println(end + "dShootingTime   end");
        //文件名
        String fileName = fileVo.getFileName();
        //文件类型
        String fileType = fileVo.getFileType();
        //获取当前时间
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式(年-月-日-时-分-秒)
        String createTime = dateFormat.format(now);//格式化然后放入字符串中

        Page<SysDoc> page = new Page<>(fileVo.getPageNo(), fileVo.getPageSize());
        QueryWrapper wrapper = new QueryWrapper();

        wrapper.eq("ddvid", one.getDvid());


        if (!StringUtils.isEmpty(fileType)) {
            QueryWrapper wrapper3 = new QueryWrapper();
            wrapper3.eq("dtChs", fileType);
            SysDoctype one1 = sysDoctypeService.getOne(wrapper3);
            if (one1 != null) {
                wrapper.eq("dDtID", one1.getDtid());
            }
        }

        if (!StringUtils.isEmpty(fileName)) {
            wrapper.like("dFilePath", fileName);
        }
        if (fileVo.getTimeType() == 0) {
            //拍摄开始时间
            if (!StringUtils.isEmpty(begin)) {
                //构建条件
                wrapper.ge("dShootingTime", begin);

            } else {
                String begin2 = createTime + " 00:00:00.000";
                wrapper.ge("dShootingTime", begin2);

            }
            //拍摄结束时间
            if (!StringUtils.isEmpty(end)) {

                //构建条件
                wrapper.le("dShootingTime", end);
            } else {
                String end2 = createTime + " 23:59:59.999";
                wrapper.le("dShootingTime", end2);

            }
        } else if (fileVo.getTimeType() == 1) {
            //上传开始时间
            if (!StringUtils.isEmpty(begin)) {
                wrapper.ge("dUploadTime", begin);
            } else {
                String begin2 = createTime + " 00:00:00.000";
                wrapper.ge("dUploadTime", begin2);

            }
            //上传结束时间
            if (!StringUtils.isEmpty(end)) {

                wrapper.le("dUploadTime", end);
            } else {
                String end2 = createTime + " 23:59:59.999";
                wrapper.le("dUploadTime", end2);

            }
        }
        wrapper.orderByDesc("dUploadTime");
        sysDocService.page(page, wrapper);
        long total = page.getTotal();
        List<SysDoc> list = page.getRecords();
        long numPage = 0;
        if (total == 0) {
            numPage = 1L;
        } else if (list.size() == 0) {
            numPage = 1L;
        } else {
            numPage = (long) Math.ceil(total / (double) fileVo.getPageSize());
        }
        return RLIST.ok().data("data", list).data("total", total).data("numPage", numPage);
    }
}
